#include <iostream>
#include "HttpServer.hpp"
#include "Usage.hpp"
#include <memory>

void HandlerHttpRequest(int sockfd)
{
    //1.读取请求 for test
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
        std::cout << buffer << "------------------------\n" << std::endl;
    }

    //2.构建一个http的响应
    std::string HttpResponse = "HTTP/1.1 200 OK\r\n";
    HttpResponse += "\r\n";
    HttpResponse += "<html><h3>tou ni xia ma</h3></html>";
    send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);

}


int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    std::unique_ptr<HttpServer> httpserver(new HttpServer(atoi(argv[1]), HandlerHttpRequest));
    httpserver->Start();
    
    return 0;
}